package com.ruoyi.energy.service.impl;

import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.energy.domain.NyTrans;
import com.ruoyi.energy.domain.vo.NyTransExcelVO;
import com.ruoyi.energy.domain.vo.WXNyTransDetial;
import com.ruoyi.energy.mapper.NyTransMapper;
import com.ruoyi.energy.mapper.NyTransPartArrsMapper;
import com.ruoyi.energy.service.INyTransService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 变压器台账Service业务层处理
 *
 * @author cjie
 * @date 2021-10-11
 */
@Service
public class NyTransServiceImpl extends LinkFileServiceImpl implements INyTransService {
    @Autowired
    private NyTransMapper nyTransMapper;

    @Autowired
    private NyTransPartArrsMapper nyTransPartArrsMapper;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询变压器台账
     *
     * @param id 变压器台账ID
     * @return 变压器台账
     */
    @Override
    public NyTrans selectNyTransById(Long id) {
        //return nyTransMapper.selectNyTransById(id);
        NyTrans result = nyTransMapper.selectNyTransVoById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询变压器台账列表
     *
     * @param nyTrans 变压器台账
     * @return 变压器台账
     */
    @Override
    public List<NyTrans> selectNyTransList(NyTrans nyTrans) {
        return nyTransMapper.selectNyTransList(nyTrans);
    }

    /**
     * 新增变压器台账
     *
     * @param nyTrans 变压器台账
     * @return 结果
     */
    @Override
    public int insertNyTrans(NyTrans nyTrans) {
        //nyTrans.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        nyTrans.setCreateTime(DateUtils.getNowDate());
        nyTrans.setCreateBy(SecurityUtils.getUsername());
        int result = nyTransMapper.insertNyTrans(nyTrans);
        if(nyTrans.getFileIdList() != null){
            linkFile(nyTrans.getFileIdList(), nyTrans.getId().toString());
        }
        return result;
    }

    /**
     * 修改变压器台账
     *
     * @param nyTrans 变压器台账
     * @return 结果
     */
    @Override
    public int updateNyTrans(NyTrans nyTrans) {
        nyTrans.setUpdateTime(DateUtils.getNowDate());
        nyTrans.setUpdateBy(SecurityUtils.getUsername());
        if(nyTrans.getFileIdList() != null){
            linkFile(nyTrans.getFileIdList(), nyTrans.getId().toString());
        }
        return nyTransMapper.updateNyTrans(nyTrans);
    }

    /**
     * 批量删除变压器台账
     *
     * @param ids 需要删除的变压器台账ID
     * @return 结果
     */
    @Override
    public int deleteNyTransByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return nyTransMapper.deleteNyTransByIds(ids);
    }

    /**
     * 删除变压器台账信息
     *
     * @param id 变压器台账ID
     * @return 结果
     */
    @Override
    public int deleteNyTransById(Long id) {
        return nyTransMapper.deleteNyTransById(id);
    }

    @Override
    public WXNyTransDetial selectWXNyTransDetialById(Long id) {
        WXNyTransDetial result = nyTransMapper.selectWXNyTransDetialById(id);
        //result.getNyTransParts().forEach();
//        result.getNyTransParts().stream().forEach(part->{
//            List<NyTransPartArrs> airs = part.getNyTransPartArrs();
//            for(NyTransPartArrs air:airs){
//                air.setNyEnteriseLoadList(nyTransPartArrsMapper.selectNyEnteriseLoadByAttrId(air.getAttrId()));
//            }
//        });
        result.getNyTransParts().stream().forEach(part->{
            part.getNyTransPartArrs().stream().forEach(air->{
                air.setNyEnteriseLoadList(nyTransPartArrsMapper.selectNyEnteriseLoadByAttrId(air.getAttrId()));
            });
        });
        return result;
    }

    @Override
    public String importData(List<NyTransExcelVO> buiRoomVOList, String meterType) {
        if (StringUtils.isNull(buiRoomVOList) || buiRoomVOList.size() == 0) {
            throw new CustomException("导入抄表数据不能为空！");
        }
        StringBuilder successMsg = new StringBuilder();
        buiRoomVOList = buiRoomVOList.stream().filter(o->StringUtils.isNotBlank(o.getNum())).collect(Collectors.toList());
        for(NyTransExcelVO nyTransVO :buiRoomVOList){
            //if exsit update
            NyTrans nyTrans = nyTransMapper.selectNyTransByNum(nyTransVO.getNum());
            if(nyTrans != null){
                BeanUtils.copyBeanProp(nyTrans,nyTransVO);
                this.updateNyTrans(nyTrans);
            }else{
                nyTrans = new NyTrans();
                BeanUtils.copyBeanProp(nyTrans,nyTransVO);
                this.insertNyTrans(nyTrans);
            }
        }
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + buiRoomVOList.size() + " 条，数据如下：");
        return successMsg.toString();
    }

    @Override
    public NyTrans selectNyTransByName(String transName,String parkNum) {
        return nyTransMapper.selectNyTransByName(transName,parkNum);
    }
}
